package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminCategoryController")
@Slf4j
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param categoryDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增分类")
    public Result addCategories(@RequestBody CategoryDTO categoryDTO){
        categoryService.addCategories(categoryDTO);
        return Result.success();
    }

    /**
     * 分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public Result<PageResult> Page(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分页查询:{}" , categoryPageQueryDTO);
        PageResult pageResult= categoryService.pageQuery(categoryPageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 启用禁用分类
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用、禁用分类")
    public Result setStatus(@PathVariable Integer status , Long id){
        log.info("禁用启用员工：{}" , id);
        categoryService.starOrStop(status , id);
        return Result.success();
    }

    /**
     * 修改分类
     * @param categoryDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改分类")
    public Result modify(@RequestBody CategoryDTO categoryDTO){
        categoryService.modify(categoryDTO);
        return Result.success();
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除id删除分类")
    public Result deleteCategory(Integer id){
        categoryService.deleteCategory(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result<List<Category>> queryClassification(Integer type){
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }

}
